package com.bit.web;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

//UDP客户端
public class UDPEchoClient {
    // 客户端的主要流程分成四步.
    // 1. 从用户这里读取输入的数据.
    // 2. 构造请求发送给服务器
    // 3. 从服务器读取响应
    // 4. 把响应写回给客户端.
    private DatagramSocket socket = null;
    private String serverIP;
    private int serverPort;

    //需要在启动客户端的时候来确定连接哪一个服务器
    public UDPEchoClient(String serverIP,int serverPort) throws SocketException {
        this.serverIP = serverIP;
        this.serverPort = serverPort;
        socket = new DatagramSocket();
    }
    public void start() throws IOException {
        Scanner scanner = new Scanner(System.in);
        while(true){
            //1.读取用户输入的数据
            System.out.println("-->");
            String request = scanner.nextLine();
            if(request.equals("exit")){
                break;
            }
            //2.构造请求发给服务器
            DatagramPacket requestPacket = new DatagramPacket(request.getBytes(),
                    request.getBytes().length, InetAddress.getByName(serverIP),serverPort);
            socket.send(requestPacket);
            //3.从服务器读取响应
            DatagramPacket responsePacket = new DatagramPacket(new byte[4096],4096);
            socket.receive(requestPacket);
            String response = new String(responsePacket.getData(),0,responsePacket.getLength()).trim();
            //4.响应数据
            System.out.println(response);
        }

    }

    public static void main(String[] args) throws IOException {
        UDPEchoClient client = new UDPEchoClient("127.0.0.1",9090);
        client.start();
    }
}
